<?php

/*
 * статический класс, отвечает за валидацию входящей информации
 */

class Control_Validator {

    private static $error = null;

    private static function returnMethod($data) {
        if (null == self::$error) {
            return $data;
        } else {

            return self::$error;
        }
    }

    public static function photoIdValid($photoId) {
        if (isset($photoId)) {
            if (preg_match('/^[0-9]{}$/', $photoId)) {
                return false;
            } else {
                return $photoId;
            }
        } else {
            return false;
        }
    }

    public static function textValid($text) {
        if (!preg_match('/^[0-9a-zA-Z\!,?._-\s]{1,300}$/i', $text)) {
            self::$error = Model_Error::getTextError();
        }
        return self::returnMethod($text);
    }

    public static function updateValid($nameAge) {
        self::nameValid($nameAge['name']);
        self::ageValid($nameAge['age']);
        return self::returnMethod($nameAge);
    }

    public static function searchValid($nameAgeSex) {
        self::sexSearchValid($nameAgeSex['sex']);
        self::ageSearchValid($nameAgeSex['age']);
        self::nameSearchValid($nameAgeSex['name']);
        return self::returnMethod($nameAgeSex);
    }

    public static function authValid($logPas) {
        self::pasValid($logPas['pas']);
        self::logValid($logPas['log']);
        return self::returnMethod($logPas);
    }

    public static function regValid($userModel) {
        self::ageValid($userModel->getUserAge());
        self::sexValid($userModel->getUserSex());
        self::pasValid($userModel->getUserPas());
        self::nameValid($userModel->getUserName());
        return self::returnMethod($userModel);
    }

    public static function getError() {
        $_SESSION['error'] = self::$error;
        return $_SESSION['error'];
    }

    public static function pasValid($userPas) {
        if (!preg_match('/^[0-9a-z_\-]{4,12}$/i', $userPas)) {
            self::$error = Model_Error::getPasswordError();
        }
    }

    public static function sexValid($userSex) {
        if (!isset($userSex)) {
            self::$error = Model_Error::getSexError();
        }
    }

    public static function sexSearchValid($userSex) {
        if (isset($userSex)) {
            return $userSex;
        } else {
            $userSex['sex'] = '';
            return $userSex;
        }
    }

    public static function ageValid($userAge) {
        if (!preg_match('/^[0-9]{2}$/', $userAge)) {
            self::$error = Model_Error::getAgeError();
        }
    }

    public static function ageSearchValid($userAge) {
        if (isset($userAge)) {
            if (preg_match('/^[0-9]{}$/', $userAge)) {
                self::$error = Model_Error::getAgeError();
            }
        }
    }

    public static function nameValid($userName) {
        if (!preg_match('/^[0-9A-Za-z]{4,12}$/', $userName)) {
            self::$error = Model_Error::getNameError();
        }
    }

    public static function nameSearchValid($userName) {
        if (isset($userName)) {
            if (preg_match('/^[0-9A-Za-z]{4,12}$/', $userName)) {
                self::$error = Model_Error::getNameError();
            }
        }
    }

    public static function logValid($userLogin) {
        if (isset($userLogin)) {
            if (!preg_match('/^[0-9A-Za-z_-]{4,31}$/', $userLogin)) {
                self::$error = Model_Error::getLoginError();
            }
        } else {
            self::$error = Model_Error::getLoginError();
        }
        return self::$error;
    }

}